import request, { post, get, put, del } from '../../utils/request.js';
import { saveToken, saveUserInfo, getToken, getUserInfo, removeToken, removeUserInfo } from '../../utils/auth.js';
import { showToast } from '../../utils/common.js';

const state = {
  token: getToken() || '',
  userInfo: getUserInfo() || null,
  addresses: []
};

const mutations = {
  SET_TOKEN(state, token) {
    state.token = token;
    saveToken(token);
  },
  SET_USER_INFO(state, userInfo) {
    state.userInfo = userInfo;
    saveUserInfo(userInfo);
  },
  SET_ADDRESSES(state, addresses) {
    state.addresses = addresses;
  },
  CLEAR_USER(state) {
    state.token = '';
    state.userInfo = null;
    state.addresses = [];
    removeToken();
    removeUserInfo();
  }
};

const actions = {
  // 登录
  async login({ commit }, params) {
    try {
      const res = await post('/api/user/login', params, { loading: true });
      commit('SET_TOKEN', res.token);
      commit('SET_USER_INFO', res.userInfo);
      return res;
    } catch (error) {
      showToast('登录失败，请重试');
      return null;
    }
  },
  
  // 退出登录
  async logout({ commit }) {
    try {
      await post('/api/user/logout');
      commit('CLEAR_USER');
      return true;
    } catch (error) {
      showToast('退出登录失败，请重试');
      return false;
    }
  },
  
  // 获取用户信息
  async getUserInfo({ commit }) {
    try {
      const res = await get('/api/user/info');
      commit('SET_USER_INFO', res);
      return res;
    } catch (error) {
      return null;
    }
  },
  
  // 获取收货地址列表
  async getAddressList({ commit }) {
    try {
      const res = await get('/api/user/address/list');
      commit('SET_ADDRESSES', res);
      return res;
    } catch (error) {
      showToast('获取地址列表失败');
      return [];
    }
  },
  
  // 添加收货地址
  async addAddress({ dispatch }, address) {
    try {
      const res = await post('/api/user/address/add', address);
      showToast('添加地址成功', 'success');
      // 刷新地址列表
      dispatch('getAddressList');
      return res;
    } catch (error) {
      showToast('添加地址失败');
      return null;
    }
  },
  
  // 编辑收货地址
  async updateAddress({ dispatch }, address) {
    try {
      const res = await put('/api/user/address/update', address);
      showToast('修改地址成功', 'success');
      // 刷新地址列表
      dispatch('getAddressList');
      return res;
    } catch (error) {
      showToast('修改地址失败');
      return null;
    }
  },
  
  // 删除收货地址
  async deleteAddress({ dispatch }, id) {
    try {
      await del('/api/user/address/delete', { id });
      showToast('删除地址成功', 'success');
      // 刷新地址列表
      dispatch('getAddressList');
      return true;
    } catch (error) {
      showToast('删除地址失败');
      return false;
    }
  }
};

const getters = {
  isLoggedIn: state => !!state.token,
  token: state => state.token,
  userInfo: state => state.userInfo,
  addresses: state => state.addresses,
  defaultAddress: state => state.addresses.find(item => item.isDefault) || (state.addresses[0] || null)
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}; 